package com.way.filemanager.navigation;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Rect;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView;

public class MyItemTextView extends TextView
{
    private final static String DIVIDE = "|";
    private final Rect mRect = new Rect();
    private String mTitle;
    private String mSubTitle;
    private TextView mSubTextView;
    private int wBlank = -1;

    public MyItemTextView(Context context) { super(context); }
    public MyItemTextView(Context context, AttributeSet attrs) { super(context, attrs); }
    public MyItemTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); }

    public void setTitle(String title, String subTitle, TextView sub)
    {
        mTitle = title;
        mSubTitle = subTitle;
        mSubTextView = sub;
        invalidate();
    }
    private void countDp()
    {
        if(wBlank < 0)
        {
            android.util.DisplayMetrics dm = getResources().getDisplayMetrics();
            wBlank = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8f, dm);
        }
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        if(mTitle == null && mSubTitle == null)
        {
            return;
        }

        TextPaint p = getPaint();

        int width = getWidth();
        int height = getHeight();
        p.setARGB(255, 0x33, 0x33, 0x33);
        if(mSubTitle == null || mSubTitle.length() <= 0)
        {
            p.getTextBounds(mTitle, 0, mTitle.length(), mRect);
            int lTitle = mRect.left;
            int tTitle = mRect.top;
            int hTitle = mRect.height();
            int yTitle = (height - hTitle)/2 - tTitle;
            int xTitle = -lTitle;
            canvas.drawText((String) TextUtils.ellipsize(mTitle, p, width, TruncateAt.END), xTitle, yTitle, p);
            return;
        }
        countDp();
        TextPaint subPaint = (mSubTextView != null) ? mSubTextView.getPaint() : p;
        subPaint.setARGB(255, 0x80, 0x80, 0x80);

        subPaint.getTextBounds(mSubTitle, 0, mSubTitle.length(), mRect);
        int wSubTitle = mRect.width();
        int ySubTitle = (height - mRect.height())/2 - mRect.top;
        int xSubTitle = -mRect.left;

        subPaint.getTextBounds(DIVIDE, 0, 1, mRect);
        int wDivide = mRect.width() + wBlank + wBlank;
        int hDivide = mRect.height();
        int yDivide = (height - hDivide)/2 - mRect.top;
        int xDivide = -mRect.left;

        int leftWidth = width - wDivide - wSubTitle;
        String title = (String) TextUtils.ellipsize(mTitle, p, leftWidth, TruncateAt.END);
        p.getTextBounds(title, 0, title.length(), mRect);

        int hTitle = mRect.height();
        int yTitle = (height - hTitle)/2 - mRect.top;
        int xTitle = -mRect.left;
        int wTitle = mRect.width();

        canvas.drawText(title, xTitle, yTitle, p);
        canvas.drawText(DIVIDE, xDivide + wTitle + wBlank, yDivide, subPaint);
        canvas.drawText(mSubTitle, xSubTitle + wTitle + wDivide, ySubTitle, subPaint);

    }

}
